<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="序号" type="index" width="100" />
       <el-table-column label="化学品名称" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.chemicalName`" :rules="formRules.chemicalName" class="mb-0px!">
            <el-input v-model="row.chemicalName" placeholder="请输入化学品名称" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="俗名或商品名" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.tradeName`" :rules="formRules.tradeName" class="mb-0px!">
            <el-input v-model="row.tradeName" placeholder="请输入俗名或商品名" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="危化品种类" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.type`" :rules="formRules.type" class="mb-0px!">
            <el-select multiple filterable v-model="row.type" placeholder="请选择危化品种类">
              <el-option
                v-for="dict in getIntDictOptions(DICT_TYPE.CRM_CHEMICALS_TYPE)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="储存量(KG)" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.storageCapacity`" :rules="formRules.storageCapacity" class="mb-0px!">
            <el-input v-model="row.storageCapacity" placeholder="请输入储存量" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="日使用量(KG)" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.dailyUsage`" :rules="formRules.dailyUsage" class="mb-0px!">
            <el-input v-model="row.dailyUsage" placeholder="请输入日使用量" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="年使用量(KG)" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.annualUsage`" :rules="formRules.annualUsage" class="mb-0px!">
            <el-input v-model="row.annualUsage" placeholder="请输入年使用量" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="用途" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.purpose`" :rules="formRules.purpose" class="mb-0px!">
            <el-input v-model="row.purpose" placeholder="请输入用途" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="采购周期" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.purchasingCycle`" :rules="formRules.purchasingCycle" class="mb-0px!">
            <el-input v-model="row.purchasingCycle" placeholder="请输入采购周期" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link>—</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3">
    <el-button @click="handleAdd" round>+ 添加化学品使用信息</el-button>
  </el-row>
</template>
<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { ChemicalsApi } from '@/api/crm/chemicals'

const props = defineProps<{
  chemicalsId: undefined // 客户化学品使用信息id（主表的关联字段）
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref([])
const formRules = reactive({
  chemicalsId: [{ required: true, message: '客户化学品使用信息id不能为空', trigger: 'blur' }],
  chemicalName: [{ required: true, message: '化学品名称不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.chemicalsId,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return;
    }
    try {
      formLoading.value = true
      formData.value = await ChemicalsApi.getChemicalsRegisterListByChemicalsId(val)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    chemicalsId: undefined,
    chemicalName: undefined,
    tradeName: undefined,
    type: undefined,
    storageCapacity: undefined,
    dailyUsage: undefined,
    annualUsage: undefined,
    purpose: undefined,
    purchasingCycle: undefined,
  }
  row.chemicalsId = props.chemicalsId
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}

defineExpose({ validate, getData })
</script>